package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.merchant.MerchantCategory;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.response.*;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.model.agent.MerchantGeneralType;
import com.zbkj.service.dao.MerchantGeneralTypeDao;
import com.zbkj.service.service.MerchantGeneralTypeService;

import com.zbkj.common.request.MerchantGeneralTypeSaveRequest;
import com.zbkj.common.request.MerchantGeneralTypeSearchRequest;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.service.util.BrandUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author Caiyc
* @description MerchantGeneralTypeServiceImpl 接口实现
* @date 2025-08-13
*/
@Service
public class MerchantGeneralTypeServiceImpl extends ServiceImpl<MerchantGeneralTypeDao, MerchantGeneralType> implements MerchantGeneralTypeService {

    @Resource
    private MerchantGeneralTypeDao dao;

    @Override
    public Boolean create(MerchantGeneralTypeSaveRequest merchantGeneralTypeSaveRequest) {
        MerchantGeneralType merchantGeneralType = new MerchantGeneralType();
        BeanUtils.copyProperties(merchantGeneralTypeSaveRequest, merchantGeneralType, "id");
        merchantGeneralType.setBrandId(BrandUtil.getBrandIdByUrl());
        boolean save = save(merchantGeneralType);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询商户联盟类型表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<MerchantGeneralType>
     * @author Caiyc
     * @since 2025-08-13
     */
    @Override
    public PageInfo<MerchantGeneralTypePageDataResponse> getList(MerchantGeneralTypeSearchRequest request, PageParamRequest pageParamRequest) {
        Integer brandId = BrandUtil.getBrandIdByUrl();
        Page<MerchantGeneralType> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        LambdaQueryWrapper<MerchantGeneralType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(MerchantGeneralType::getBrandId, brandId);
        lambdaQueryWrapper.eq(MerchantGeneralType::getIsDel, false);
        if (request.getStatus() != null) {
            lambdaQueryWrapper.eq(MerchantGeneralType::getStatus, request.getStatus());
        }
        lambdaQueryWrapper.orderByAsc(MerchantGeneralType::getSort);
        lambdaQueryWrapper.orderByDesc(MerchantGeneralType::getCreateTime);
        List<MerchantGeneralType> merchantGeneralTypes = dao.selectList(lambdaQueryWrapper);
        List<MerchantGeneralTypePageDataResponse> merchantGeneralTypeResponses = new ArrayList<>();

        merchantGeneralTypes.stream().forEach(x->{
            MerchantGeneralTypePageDataResponse merchantGeneralTypeResponse = new MerchantGeneralTypePageDataResponse();
            BeanUtils.copyProperties(x, merchantGeneralTypeResponse);
            // 平台名称
            if (x.getPid() != null && x.getPid() > 0) {
                MerchantGeneralType category = this.getNameById(x.getPid());
                merchantGeneralTypeResponse.setParentName(category.getName());
            }
            merchantGeneralTypeResponses.add(merchantGeneralTypeResponse);
        });
        return CommonPage.copyPageInfo(page, merchantGeneralTypeResponses);
    }

    private MerchantGeneralType getNameById(Integer pid) {
        LambdaQueryWrapper<MerchantGeneralType> lqw = Wrappers.lambdaQuery();
        lqw.select(MerchantGeneralType::getName);
        lqw.eq(MerchantGeneralType::getIsDel, false);
        lqw.eq(MerchantGeneralType::getId, pid);
        return getOne(lqw);
    }

    /**
    * 修改商户联盟类型表
    * @param request
    * @return
    */
    @Override
    public boolean update(MerchantGeneralTypeSaveRequest request) {
        if (ObjectUtil.isNull(request.getId()) || request.getId() < 1) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "请传入商户联盟类型ID");
        }
        getByIdException(request.getId());
        MerchantGeneralType merchantGeneralType = new MerchantGeneralType();
        BeanUtils.copyProperties(request, merchantGeneralType);
        return updateById(merchantGeneralType);
    }

    private MerchantGeneralType getByIdException(Integer id) {
        MerchantGeneralType merchantGeneralType = getById(id);
        if (ObjectUtil.isNull(merchantGeneralType)) {
            throw new AppException("商户联盟类型不存在");
        }
        if (merchantGeneralType.getIsDel()) {
            throw new AppException("商户联盟类型不存在");
        }
        return merchantGeneralType;
    }

    /**
    * 批量删除
    * @param idList
    * @return
    */
    @Override
    public boolean batchDelete(List<Integer> idList) {
        LambdaUpdateWrapper<MerchantGeneralType> luw = Wrappers.lambdaUpdate();
        luw.set(MerchantGeneralType::getIsDel, true);
        luw.in(MerchantGeneralType::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除商户联盟类型失败"));
        }
        return true;
    }

    /**
    * 查询详情
    * @param id
    * @return
    */
    @Override
    public MerchantGeneralTypeDetailResponse getDetails(Integer id) {
        LambdaQueryWrapper<MerchantGeneralType> lqw = Wrappers.lambdaQuery();
        lqw.eq(MerchantGeneralType::getIsDel, 0);
        lqw.eq(MerchantGeneralType::getBrandId, BrandUtil.getBrandIdByUrl());
        lqw.eq(MerchantGeneralType::getId, id);
        lqw.last("limit 1");
        MerchantGeneralType merchantGeneralType = this.getOne(lqw);
        if (ObjectUtil.isNotNull(merchantGeneralType)) {
            MerchantGeneralTypeDetailResponse response = new MerchantGeneralTypeDetailResponse();
            BeanUtils.copyProperties(merchantGeneralType, response);
            // 平台名称
            if (merchantGeneralType.getPid() != null && merchantGeneralType.getPid() > 0) {
                MerchantGeneralType category = this.getNameById(merchantGeneralType.getPid());
                response.setParentName(category.getName());
            }
            return response;
        }
        return null;
    }

    @Override
    public List<MerchantGeneralTypeListResponse> getPlatformList() {
        LambdaQueryWrapper<MerchantGeneralType> lqw = Wrappers.lambdaQuery();
        lqw.eq(MerchantGeneralType::getIsDel, false);
        lqw.eq(MerchantGeneralType::getBrandId, 0);
        lqw.eq(MerchantGeneralType::getStatus, 1);
        lqw.orderByAsc(MerchantGeneralType::getSort);
        lqw.orderByDesc(MerchantGeneralType::getCreateTime);
        List<MerchantGeneralType> merchantCategories = dao.selectList(lqw);
        if (CollUtil.isEmpty(merchantCategories)) {
            return new ArrayList<>();
        }
        List<MerchantGeneralTypeListResponse> communityStatRecordResponses = new ArrayList<>();
        for (MerchantGeneralType merchantCategory : merchantCategories) {
            MerchantGeneralTypeListResponse communityStatRecordResponse = new MerchantGeneralTypeListResponse();
            BeanUtils.copyProperties(merchantCategory, communityStatRecordResponse);
            communityStatRecordResponses.add(communityStatRecordResponse);
        }
        return communityStatRecordResponses;
    }

    @Override
    public List<MerchantGeneralTypeFrontListResponse> allListH5() {
        LambdaQueryWrapper<MerchantGeneralType> lqw = Wrappers.lambdaQuery();
        lqw.eq(MerchantGeneralType::getIsDel, false);
        lqw.eq(MerchantGeneralType::getStatus, 1);
        lqw.eq(MerchantGeneralType::getBrandId, BrandUtil.getBrandIdByUrl());
        lqw.orderByAsc(MerchantGeneralType::getSort);
        lqw.orderByDesc(MerchantGeneralType::getCreateTime);
        List<MerchantGeneralType> merchantCategories = dao.selectList(lqw);
        if (CollUtil.isEmpty(merchantCategories)) {
            return new ArrayList<>();
        }
        List<MerchantGeneralTypeFrontListResponse> communityStatRecordResponses = new ArrayList<>();
        for (MerchantGeneralType merchantCategory : merchantCategories) {
            MerchantGeneralTypeFrontListResponse communityStatRecordResponse = new MerchantGeneralTypeFrontListResponse();
            BeanUtils.copyProperties(merchantCategory, communityStatRecordResponse);
            communityStatRecordResponses.add(communityStatRecordResponse);
        }
        return communityStatRecordResponses;
    }

    @Override
    public List<MerchantGeneralTypeFrontListResponse> allListBrand() {
        LambdaQueryWrapper<MerchantGeneralType> lqw = Wrappers.lambdaQuery();
        lqw.eq(MerchantGeneralType::getIsDel, false);
        lqw.eq(MerchantGeneralType::getStatus, 1);
        lqw.eq(MerchantGeneralType::getBrandId, BrandUtil.getBrandIdByUrl());
        lqw.orderByAsc(MerchantGeneralType::getSort);
        lqw.orderByDesc(MerchantGeneralType::getCreateTime);
        List<MerchantGeneralType> merchantCategories = dao.selectList(lqw);
        if (CollUtil.isEmpty(merchantCategories)) {
            return new ArrayList<>();
        }
        List<MerchantGeneralTypeFrontListResponse> communityStatRecordResponses = new ArrayList<>();
        for (MerchantGeneralType merchantCategory : merchantCategories) {
            MerchantGeneralTypeFrontListResponse communityStatRecordResponse = new MerchantGeneralTypeFrontListResponse();
            BeanUtils.copyProperties(merchantCategory, communityStatRecordResponse);
            communityStatRecordResponses.add(communityStatRecordResponse);
        }
        return communityStatRecordResponses;
    }

    @Override
    public List<MerchantGeneralType> getListByIds(List<Integer> ids) {
        LambdaQueryWrapper<MerchantGeneralType> lqw = Wrappers.lambdaQuery();
        lqw.eq(MerchantGeneralType::getIsDel, false);
        lqw.in(MerchantGeneralType::getId, ids);
        lqw.orderByAsc(MerchantGeneralType::getSort);
        lqw.orderByDesc(MerchantGeneralType::getId);
        return dao.selectList(lqw);
    }

}

